﻿using ConfExpert.Forms.Base;
using DevExpress.Pdf;
using System;
using System.IO;
using System.Windows.Forms;
using DevTeam;

namespace ConfExpert.Forms.Lojistik
{
    public partial class ParseMagazaDagilimi : XForm
    {
        public ParseMagazaDagilimi()
        {
            InitializeComponent();
        }

        private void ParseMagazaDagilimi_Load(object sender, EventArgs e)
        {
            btnDonustur.ItemClick += btnDonustur_ItemClick;
        }

        void btnDonustur_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (cmbCustomer.EditValue.ToStr())
            {
                case "ALTEA":
                    using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
                        if (openFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            if (openFileDialog1.OpenFile() != null)
                            {
                                string text = ExtractTextFromPDF(openFileDialog1.FileName);

                                SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(System.Globalization.CultureInfo.CurrentCulture);
                                SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0];

                                int rowOffset = 0;
                                foreach (string str in text.Split(System.Environment.NewLine.ToCharArray()))
                                {
                                    string[] words = str.Split("|".ToCharArray());
                                    if (words.Length < 5)
                                        continue;

                                    int colOffset = 0;
                                    if (rowOffset < 2 || words[4].Trim().ToInt() > 0)
                                    {
                                        foreach (string word in words)
                                            worksheet.Cells[rowOffset, colOffset++].Value = word.Trim();

                                        rowOffset++;
                                    }
                                }

                                worksheet.Cells[2, 0].Select();
                                worksheet.WindowInfo.FreezePanes = true;
                                worksheet.UsedRange.Columns.AutoFit();
                                for (int col = 0; col < worksheet.UsedRange.ColumnCount; col++)
                                    worksheet.Cells[1, col].ColumnWidth *= 1.13;

                                System.IO.MemoryStream memoryStream2 = new System.IO.MemoryStream();
                                workbook.SaveToStream(memoryStream2, SpreadsheetGear.FileFormat.OpenXMLWorkbook);

                                // Close the workbook.
                                workbook.Close();

                                string DosyaAdi = "Altea" + ((++ver).ToString()) + ".xlsx";
                                byte[] BLOB = memoryStream2.ToArray();
                                string FileToSend = ByteArrayToFile3(DosyaAdi, BLOB);
                                System.Diagnostics.Process process = new System.Diagnostics.Process();
                                process.StartInfo.FileName = FileToSend;
                                process.Start();
                            }
                        }
                    break;


            }
        }
        int ver = 0;

        public string ByteArrayToFile3(string filePath, byte[] content)
        {
            string FullPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache) + "\\" + filePath;
            using (FileStream fs = new FileStream(FullPath, FileMode.Create, FileAccess.Write))
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                bw.Write(content);
                bw.Flush();
            }
            return FullPath;
        }

        string ExtractTextFromPDF(string filePath)
        {
            string documentText = "";
            try
            {
                using (FileStream documentStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor())
                {
                    documentProcessor.LoadDocument(documentStream);
                    documentText = documentProcessor.Text;
                }
            }
            catch { }
            return documentText;
        }
    }
}
